package com.example.spring20241027.mapper;

import com.example.spring20241027.model.Userinfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserinfoMapper {


    // 查询
    @Select("select * from userinfo;")
    public List<Userinfo> selectUserinfoList();

    // 参数查询
    @Select("select * from userinfo where id = #{id}")
    public Userinfo selectUserinfoById(Integer id);

    // 参数重命名
    @Select("select * from userinfo where username = #{name}")
    public List<Userinfo> selectUserinfoByUsername(@Param("name") String username);


    // 查询字段重命名
    @Select("select id,username,password,age,gender,phone,delete_flag as deleteFlag," +
            "create_time as createTime , update_time as updateTime from userinfo;")
    public List<Userinfo> selectUserinfoListAs();

    // 注解字段匹配
    @Results(id = "resultMap" , value = {
            @Result(column = "delete_flag" ,property = "deleteFlag"),
            @Result(column = "create_time" ,property = "createTime"),
            @Result(column = "update_time" ,property = "updateTime")
    })
    @Select("select * from userinfo;")
    public List<Userinfo> selectUserinfoListAnnotation();

    // 查询
    @ResultMap(value = "resultMap")
    @Select("select * from userinfo;")
    public List<Userinfo> selectUserinfoList2();


    // useGeneratedKeys取出mysql内部自增的主键赋值给指定的属性keyProperty，默认不取出
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) values (#{username},#{password},#{age},#{gender},#{phone})")
    public int insertUserinfo(Userinfo userinfo);


    // 使用注解Parma重命名，在获取对象属性时，需要 对象.属性
    @Insert("insert into userinfo (username,password,age,gender,phone) values (#{user.username},#{user.password},#{user.age},#{user.gender},#{user.phone})")
    public int insertUser(@Param("user") Userinfo userinfo);
}
